<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: ActiveTest::Subject</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">ActiveTest::Subject</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/lib/active_test/subject_rb.html">
                lib/active_test/subject.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="Base.html">
                Base
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <h2>Subjects</h2>
<p>
Each type of test, whether it is for a controller, ActiveRecord model, view
or helper, is considered the subject of an ActiveTest. Subjects extend <a
href="Base.html">ActiveTest::Base</a> to allow specific forms of testing,
such as controller, stress, integration or record tests. They come with an
array of methods and attachments into both the <a
href="../Test/Unit.html">Test::Unit</a> and Rails libraries to allow DRY or
rich testing.
</p>
<p>
As a general rule, each subject should be its own suite and would look
something like this:
</p>
<pre>
  class SessionControllerTest &lt; ActiveTest::Controller

    setup_controller :login_as =&gt; :quentin
    test_require_login_for :index
</pre>
<h2>Dynamic Methods</h2>
<p>
Instead of writing highly repetitive test cases, you can use dynamic class
methods to generate the specific conditions which repeat and provide a hash
of options or parameters to affect that particular test.
</p>
<p>
They are dynamic to
</p>
<h2>Anatomy of Dynamic Methods</h2>
<p>
It will not fill everyone with warm fuzzy feelings knowing that their tests
are in the hands of such a chameleonic test suite. To ease some of the
tension, there is actually very little magic being practised. It&#8217;s
quite elementary.
</p>
<p>
Here is the call stack in shorthand:
</p>
<pre>
    test_index_responds_with_index_items
      =&gt; method_missing(&quot;test_index_responds_with_index_items&quot;)
        =&gt; responds_with(:index, :index_items)
          =&gt; define_method &quot;test_should_index_items&quot;

    test_should_index_items
      =&gt; index_items(:index, parameters)
        =&gt; get :index, parameters
      =&gt; assert_index_success(parameters)
        =&gt; method_missing(&quot;assert_index_success&quot;)
          =&gt; send assert_get_success(:index, parameters)
            =&gt; assert_response :success
            =&gt; assert_template 'index'
            =&gt; assert_assigned @model_plural.to_sym
</pre>
<p>
So, you see, it is very easy to overwrite any stage of the process. If you
have an action called &#8216;track&#8217; that needs a special response,
write a custom <tt>assert_track_success</tt>. If, on the other hand, the
operation needs to take a special parameter, you can define
<tt>track_items</tt> and use it as part of your dynamic methods. If you
don&#8217;t like the way <tt>responds_with</tt> or fails_on handle things,
you can rewrite them too.
</p>
<h2>Reign In The Terror With Options</h2>
<p>
Along the way, of course, these dynamic methods will not be able to deal
with every situation. They are not meant to do that either, but they are
meant to deal with most cases where CRUD operations are being used in a
semi-RESTful manner. The two dynamic method generators,
<tt>responds_with</tt> and &lt;code&gt;
</p>

    </div>


   </div>


  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>